﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace OxyPlotDemo
{
    public class SensorData : INotifyPropertyChanged
    {
        [DllImport("workstation.dll", EntryPoint = "send_cmd", CallingConvention = CallingConvention.Cdecl)]
        public static extern double send_cmd(int cmd_type);
        public event PropertyChangedEventHandler PropertyChanged;
        private double sensorValue;
        private string status;
        private int typeNumber;
        public double SensorValue
        {
            get { return sensorValue; }
            set
            {
                sensorValue = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("SensorValue"));
                }
            }
        }
        public string Status
        {
            get { return status; }
            set
            {
                status = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Status"));
                }
            }
        }
        public void SetTypeNumber(int n)
        {
            typeNumber = n;
        }
        public void GetData()
        {
            if (typeNumber == 2 || typeNumber == 3 || typeNumber == 6 || typeNumber == 8)
                SensorValue = Math.Round(send_cmd(typeNumber + 10), 0);
            else
                SensorValue = Math.Round(send_cmd(typeNumber + 10), 1);
            GetStatus();
        }
        public void ResetData()
        {
            if (typeNumber == 2 || typeNumber == 3 || typeNumber == 6 || typeNumber == 8)
                SensorValue = Math.Round(send_cmd(typeNumber + 20), 0);
            else
                SensorValue = Math.Round(send_cmd(typeNumber + 20), 1);
            GetStatus();
        }
        public void GetStatus()
        {
            switch (typeNumber)
            {
                case 0:
                    if (SensorValue >= 35)
                        Status = "高温";
                    else if (SensorValue <= -7)
                        Status = "低温";
                    else
                        Status = "正常";
                    break;
                case 1:
                    if (SensorValue > 60)
                        Status = "潮湿";
                    else
                        Status = "正常";
                    break;
                case 2:
                    if (SensorValue <= 50)
                        Status = "优";
                    else if (SensorValue <= 100)
                        Status = "良";
                    else if (SensorValue <= 150)
                        Status = "轻度污染";
                    else if (SensorValue <= 200)
                        Status = "中度污染";
                    else if (SensorValue <= 300)
                        Status = "重度污染";
                    else
                        Status = "严重污染";
                    break;
                case 3:
                    if (SensorValue <= 2)
                        Status = "一级";
                    else if (SensorValue <= 4)
                        Status = "二级";
                    else if (SensorValue <= 6)
                        Status = "三级";
                    else if (SensorValue <= 9)
                        Status = "四级";
                    else
                        Status = "五级";
                    break;
                case 4:
                    if (SensorValue <= 20)
                        Status = "很静";
                    else if (SensorValue <= 40)
                        Status = "安静";
                    else if (SensorValue <= 60)
                        Status = "一般";
                    else if (SensorValue <= 70)
                        Status = "吵闹";
                    else if (SensorValue <= 90)
                        Status = "很吵";
                    else
                        Status = "吵闹加剧";
                    break;
                case 5:
                    if (SensorValue >= 1020)
                        Status = "高";
                    else if (SensorValue < 960)
                        Status = "低";
                    else
                        Status = "正常";
                    break;
                case 6:
                    if (SensorValue >= 160)
                        Status = "过快";
                    else
                        Status = "正常";
                    break;
                case 7:
                    if (SensorValue > 37.4)
                        Status = "体温过高";
                    else if (SensorValue < 36)
                        Status = "体温过低";
                    else
                        Status = "正常";
                    break;
                case 8: break;
                default: break;
            }
        }
    }
}
